//
//  ViewController.m
//  微博表情键盘
//
//  Created by zxy on 2019/8/9.
//  Copyright © 2019 zxy. All rights reserved.
//

#import "ViewController.h"
#import "EmoticonModel.h"
#import "EmoticonKeyBoard/EmoticonInputView.h"
#import "EMoticonTextParser.h"  //表情解析器
@interface ViewController ()<EmoticonInputViewDelegate>
@property(nonatomic,strong)YYTextView *txtView;
@property(nonatomic,strong)UIView *toolBar;
@property(nonatomic,strong)UIButton *emoticonButton;//工具条表情按钮
@end

@implementation ViewController

-(UIView *)toolBar{
    if(!_toolBar){
        _toolBar = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 40, SCREEN_WIDTH, 40)];
        
        UIButton *pictureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [pictureBtn setImage:[XHHelper imageFromBundleName:@"ResourceWeibo" imageName:@"compose_toolbar_picture"] forState:UIControlStateNormal];
        [pictureBtn setImage:[XHHelper imageFromBundleName:@"ResourceWeibo" imageName:@"compose_toolbar_picture_highlighted"] forState:UIControlStateHighlighted];
        
        UIButton *AtBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [AtBtn setImage:[XHHelper imageFromBundleName:@"ResourceWeibo" imageName:@"compose_mentionbutton_background"] forState:UIControlStateNormal];
        [AtBtn setImage:[XHHelper imageFromBundleName:@"ResourceWeibo" imageName:@"compose_mentionbutton_background_highlighted"] forState:UIControlStateHighlighted];
       
        UIButton *TopicBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [TopicBtn setImage:[XHHelper imageFromBundleName:@"ResourceWeibo" imageName:@"compose_trendbutton_background"] forState:UIControlStateNormal];
        [TopicBtn setImage:[XHHelper imageFromBundleName:@"ResourceWeibo" imageName:@"compose_trendbutton_background_highlighted"] forState:UIControlStateHighlighted];
        
    
        UIButton *emoticonBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [emoticonBtn setImage:[XHHelper imageFromBundleName:@"ResourceWeibo" imageName:@"compose_emoticonbutton_background"] forState:UIControlStateNormal];
        [emoticonBtn setImage:[XHHelper imageFromBundleName:@"ResourceWeibo" imageName:@"compose_emoticonbutton_background_highlighted"] forState:UIControlStateHighlighted];
        [emoticonBtn addTarget:self action:@selector(emoticonBtnClick) forControlEvents:UIControlEventTouchUpInside];
        self.emoticonButton = emoticonBtn;
        
        UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [addBtn setImage:[XHHelper imageFromBundleName:@"ResourceWeibo" imageName:@"message_add_background"] forState:UIControlStateNormal];
        [addBtn setImage:[XHHelper imageFromBundleName:@"ResourceWeibo" imageName:@"message_add_background_highlighted"] forState:UIControlStateHighlighted];
        
        [_toolBar addSubview:pictureBtn];
        [_toolBar addSubview:AtBtn];
        [_toolBar addSubview:TopicBtn];
        [_toolBar addSubview:emoticonBtn];
        [_toolBar addSubview:addBtn];
        
        [pictureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.equalTo(self->_toolBar);
            make.width.mas_equalTo(SCREEN_WIDTH / 5);
        }];
        
        [AtBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(pictureBtn.mas_right);
            make.top.bottom.equalTo(pictureBtn);
            make.width.mas_equalTo(pictureBtn);
        }];
        
        [TopicBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(AtBtn.mas_right);
            make.top.bottom.equalTo(AtBtn);
            make.width.mas_equalTo(pictureBtn);
        }];
        
        [emoticonBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(TopicBtn.mas_right);
            make.top.bottom.equalTo(AtBtn);
            make.width.mas_equalTo(TopicBtn);
        }];
        
        [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(emoticonBtn.mas_right);
            make.top.bottom.equalTo(emoticonBtn);
            make.width.mas_equalTo(pictureBtn);
        }];
        _toolBar.backgroundColor = UIColorHex(eeeeee);
    }
    return _toolBar;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTextView];
    
}

#pragma mark -初始化textView
-(void)initTextView{
    _txtView = [[YYTextView alloc]initWithFrame:CGRectMake(0,64,SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 40)];
    _txtView.placeholderText = @"分享新鲜事";
    _txtView.placeholderFont = [UIFont systemFontOfSize:14];
    _txtView.textContainerInset = UIEdgeInsetsMake(12, 16, 12, 16);;
    _txtView.font = [UIFont systemFontOfSize:17];
    //表情解析器,将[色] 解析成 😍显示
    _txtView.textParser = [EmoticonTextParser new];
    [self.view addSubview:_txtView];
    [_txtView becomeFirstResponder];
    //添加底部工具条
    [self.view addSubview:self.toolBar];
    
    //监听键盘
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

#pragma mark -切换到表情键盘
-(void)emoticonBtnClick{
    if(_txtView.inputView){//切换到系统默认键盘
        _txtView.inputView = nil;
        [_txtView reloadInputViews];
        [_txtView becomeFirstResponder];
        [self.emoticonButton setImage:[XHHelper imageFromBundleName:@"ResourceWeibo" imageName:@"compose_emoticonbutton_background"] forState:UIControlStateNormal];
        [self.emoticonButton setImage:[XHHelper imageFromBundleName:@"ResourceWeibo" imageName:@"compose_emoticonbutton_background_highlighted"]  forState:UIControlStateHighlighted];
    }
    else{//切换到表情键盘
        EmoticonInputView *emoticonView = [EmoticonInputView sharedEmInputView];
        emoticonView.delegate = self;
        _txtView.inputView = emoticonView;
        [_txtView reloadInputViews];
        [_txtView becomeFirstResponder];
        [self.emoticonButton setImage:[XHHelper imageFromBundleName:@"ResourceWeibo" imageName:@"compose_keyboardbutton_background"] forState:UIControlStateNormal];
        [self.emoticonButton setImage:[XHHelper imageFromBundleName:@"ResourceWeibo" imageName:@"compose_keyboardbutton_background_highlighted"]  forState:UIControlStateHighlighted];
    }
}

#pragma mark -键盘将要显示
-(void)keyBoardWillShow:(NSNotification *)no{
    //弹出系统键盘
    CGFloat duration = [no.userInfo[UIKeyboardAnimationDurationUserInfoKey]floatValue];
    CGRect rect = [no.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    [UIView animateWithDuration:duration animations:^{
        self.toolBar.top = SCREEN_HEIGHT - rect.size.height - 40;
    }];
    [self.view layoutIfNeeded];
}

#pragma mark -键盘将要隐藏
-(void)keyBoardWillHide:(NSNotification *)no{
    CGFloat duration = [no.userInfo[UIKeyboardAnimationDurationUserInfoKey]floatValue];
    [UIView animateWithDuration:duration animations:^{
        self.toolBar.top = SCREEN_HEIGHT - 40;
    }];
    [self.view layoutIfNeeded];
}

#pragma mark -EmoticonInputViewDelegate
-(void)emoticonInputDidTapText:(NSString *)text{
    if(text.length){
        [self.txtView replaceRange:self.txtView.selectedTextRange withText:text];
    }
}
-(void)emoticonInputDidTapBackspace{
    [self.txtView deleteBackward];
}
@end
